<?php
use Core\Di;
use think\Db;
use Log\SeasLog;

class Bootstrap extends Yaf\Bootstrap_Abstract
{

    /**
     * 常用类加入DI容器
     */
    public function _initAddDi()
    {
        // 系统配置
        $appConfig = Yaf\Application::app()->getConfig()->toArray();
//        $db_config = Yaconf::get('db.'.strtolower(ENV))??Yaconf::get('db.default');
//        $redis_config =Yaconf::get('redis.'.strtolower(ENV))??Yaconf::get('redis.default');
//        $appConfig = array_merge($appConfig,$db_config,$redis_config);

        Di::set('config', $appConfig);
        // 请求类
        $Request = new \Core\Request();

        Di::set('request', $Request);
        // 错误信息表
        $messages = require(APP_PATH .'/public/messagebasic.php');
        if(is_array($messages)){
            Di::set('message', $messages);
        }
    }

    /**
     * 加载日志
     */
    public function _initLog()
    {
        \SeasLog::setBasePath(Di::get('config')['seaslog']['path']);
    }


    /**
     * 加载控制器基类
     */
    public function _initCore()
    {
        ini_set('yaf.library', APP_PATH);
        Yaf\Loader::import(CORE_PATH . '/Controller.php');
        Yaf\Loader::import(LIB_PATH . '/Controller/App.php');
    }

    /**
     *  加载DB类
     */
    public function _initDb()
    {
        $appConfig = Yaf\Application::app()->getConfig();
//        $appConfig = Yaconf::get('db.'.strtolower(ENV))??Yaconf::get('db.default');
        think\Db::setConfig([
            'type'            => $appConfig['TYPE'],
            'hostname'        => $appConfig['READ_HOST'],
            'database'        => $appConfig['Default'],
            'username'        => $appConfig['WRITE_USER'],
            'password'        => $appConfig['WRITE_PSWD'],
            'hostport'        => $appConfig['WRITE_PORT'],
            'prefix'          => 't_',
            'debug'           => true,
//            'resultset_type'  => '\think\Collection'
        ]);
        Db::listen(function ($sql, $time, $explain) {
            SeasLog::setSqlLog($sql . ' | ' . $time);
        });
    }

    /**
     * 加载助手函数
     */
    public function _initCommon()
    {
        Yaf\Loader::import(FUNC_PATH . '/Common.php');
        Yaf\Loader::import(FUNC_PATH . '/F_Array.php');
        Yaf\Loader::import(FUNC_PATH . '/F_Basic.php');
        Yaf\Loader::import(FUNC_PATH . '/F_File.php');
        Yaf\Loader::import(FUNC_PATH . '/F_Img.php');
        Yaf\Loader::import(FUNC_PATH . '/F_Network.php');
        Yaf\Loader::import(FUNC_PATH . '/F_PY.php');
        Yaf\Loader::import(FUNC_PATH . '/F_String.php');
        Yaf\Loader::import(FUNC_PATH . '/F_Validate.php');
    }

    /**
     * 加载缓存
     */
//    public function _initCache()
//    {
//        $appConfig = Di::get('config');
//        $Redis = new \Cache\Redis([
//            'host'       => $appConfig['redis_host'],
//            'port'       => $appConfig['redis_port'],
//            'password'   => $appConfig['redis_auth']
//        ]);
//        Di::set('Redis', $Redis);
//    }

    /**
     * 加载路由
     */
    public function _initRoute()
    {
        $router = Yaf\Dispatcher::getInstance()->getRouter();
        //旧版本拦截
        $route2 = new Yaf\Route\Regex(
            '#apps/#',
            array('module' => 'apps', 'controller' => 'appversion', 'action' => 'shielding'));
        $router->addRoute('route2', $route2);

        //兼容旧版本更新路由
        $route1 = new Yaf\Route\Regex(
            '#apps/appversion#',
            array('module' => 'apps', 'controller' => 'appversion', 'action' => 'index'));
        $router->addRoute('route1', $route1);

        //关于页面路由
        $mobile_about = new Yaf\Route\Regex(
            '#mobile/about#',
            array('module' => 'mobile', 'controller' => 'information', 'action' => 'introduce'));
        $router->addRoute('mobile_about', $mobile_about);
    }

    public function _initPlugin(Yaf\Dispatcher $dispatcher) {
        $router = new RouterPlugin();
        $dispatcher->registerPlugin($router);
    }


}